package com.wanmait.wanmarecruit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.wanmarecruit.annotation.ManageLogAnnotation;
import com.wanmait.wanmarecruit.service.ManagerService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanmait.wanmarecruit.entity.Manager;
import com.wanmait.wanmarecruit.mapper.ManagerMapper;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = Exception.class)
public class ManagerServiceImpl extends ServiceImpl<ManagerMapper, Manager> implements ManagerService {
    @Resource
    private ManagerMapper managerMapper;

    @Override
    public Manager checkNameAndPass(Manager manager) {
        LambdaQueryWrapper<Manager> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Manager::getName,manager.getName());
        queryWrapper.eq(Manager::getPass,manager.getPass());
        return this.getOne(queryWrapper);
    }

    @Override
    public void updateLoginTime(Integer id) {
        Date date = new Date();
        Manager manager = new Manager();
        manager.setId(id);
        manager.setLoginTime(date);
        this.updateById(manager);
    }

    @Override
    public PageInfo<Manager> findAllManagerByPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        return new PageInfo<>(this.list());
    }

    @Override
    public Manager selectOneById(Integer id) {
        return managerMapper.selectOneById(id);
    }

    @Override
    @ManageLogAnnotation(module = "管理员管理",info = "新增了一个管理员")
    public void insertOneManager(Manager manager) {
        this.save(manager);
    }

    @Override
    public boolean checkNameIsExist(String name) {
        LambdaQueryWrapper<Manager> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Manager::getName,name);
        return this.getOne(queryWrapper)==null;
    }

    @Override
    @ManageLogAnnotation(module = "管理员管理",info = "删除了一个管理员")
    public void deleteOneManager(Integer id) {
        this.removeById(id);
    }
}
